/*
 * swTWI_MCP342x.h
 *
 *  Created on: 12-09-2014
 *      Author: roper123pl
 *
 */

#ifndef SWTWI_MCP342X_H_
#define SWTWI_MCP342X_H_



#include <Arduino.h>



/*
 * Address of device
 * Base address for MCP342x is 0x68
 * For MCP3421, MCP3425, MCP3426 is factory programmed 0x68 thru 0x6F
 * For others programmed by A0 and A1 lines.
 */
#define MCP342x_BASE_ADDRESS	0x68

/*
 * Conversion mode definitions
 */
#define MCP342x_MODE_ONESHOT	0x00
#define MCP342x_MODE_CONTINUOUS	0x10

/*
 * Channel definitions
 *
 * MCP3421 & MCP3425 have only the one channel and ignore this param
 * MCP3422, MCP3423, MCP3426 & MCP3427 have two channels and treat 2 & 3 as repeats of 0 & 1 respectively
 * MCP3424 & MCP3428 have all four channels
 */
#define MCP342x_CHANNEL_0 0x00
#define MCP342x_CHANNEL_1 0x20
#define MCP342x_CHANNEL_2 0x40
#define MCP342x_CHANNEL_3 0x60
#define MCP342x_CHANNEL_MASK 0x60

/*
 * Sample size definitions
 *
 */
#define MCP342x_SIZE_12BIT	0x00
#define MCP342x_SIZE_14BIT	0x04
#define MCP342x_SIZE_16BIT	0x08
#define MCP342x_SIZE_18BIT	0x0C
#define MCP342x_SIZE_MASK	0x0C

/*
 * Programmable Gain definitions
 */
#define MCP342x_GAIN_1X	0x00
#define MCP342x_GAIN_2X	0x01
#define MCP342x_GAIN_4X	0x02
#define MCP342x_GAIN_8X	0x03
#define MCP342x_GAIN_MASK 0x03

/*
 * /RDY bit definition
 */
#define MCP342x_RDY	0x80



class swTWI_MCP342x {
  public:
	swTWI_MCP342x();

	uint8_t begin();
	uint8_t begin(uint8_t address);

	uint8_t setConfig();
	uint8_t setConfig(uint8_t mode);
	uint8_t getConfig();

	uint8_t startConversation();
	uint8_t startConversation(uint8_t channel);

	uint8_t readADC(int32_t* data, uint8_t iter);
	uint8_t readADC(int32_t* data);

    private:
        uint8_t _devAddr;
        uint8_t _cfgReg;
};

#endif /* SWTWI_MCP342X_H_ */
